package com.sun.weekend.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sunleilei
 */

public abstract class AdapterBase extends android.widget.BaseAdapter{

    protected Context mContext;
    protected List items = new ArrayList();
    public AdapterBase(Context mContext) {
        this.mContext = mContext;
    }

    public void appendData(Object object) {
        items.add(object);
        notifyDataSetChanged();
    }

    public void clear() {
        items.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(getLayoutId(),null);
        }
        setViews(convertView,position);
        return convertView;
    }
    protected abstract int getLayoutId();
    protected abstract void setViews(View convertView,int position);
}
